package com.bsj.media.web;

import com.bsj.media.init.CloseThread;
import com.bsj.media.init.Gb28181RtpCachedThread;
import com.bsj.media.init.NettyConnectUntil;
import com.bsj.media.init.RestActionUtil;
import com.bsj.media.network.Http2NettyUntil;
import com.bsj.media.network.HttpNettyUntil;
import com.bsj.media.network.udpUntil.UDPBackRTPHandle;
import com.bsj.media.network.udpUntil.UDPRTPHandle;
import com.bsj.media.queue.PushMsgQueue;
import com.bsj.media.tool.HttpPlayBackTool;
import com.bsj.media.tool.HttpPlayVideoTool;
import com.bsj.media.tool.HttpTool;
import com.bsj.media.tool.VideoCmdUntil;
import com.bsj.power.common.config.cached.RedisCached;
import com.bsj.power.common.config.redis.RedisConstant;
import com.bsj.power.common.util.SpringUtil;
import com.bsj.tools.netty.UdpServiceUntil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;

/**
 * @author yinhao
 * @version 1.0
 * @description 启动类
 * @date 2024/6/17
 */
@Slf4j
@Component
@Import(SpringUtil.class)
public class Action implements CommandLineRunner {

    @Autowired
    private HttpNettyUntil httpNettyUntil;

    @Autowired
    private Http2NettyUntil http2NettyUntil;

    @Autowired
    private PushMsgQueue pushMsgQueue;

    @Autowired
    private VideoCmdUntil videoCmdUntil;

    @Autowired
    private RestActionUtil restActionUtil;

    @Override
    public void run(String... args) throws Exception {
        //启动时清除所有直播状态
        restActionUtil.reset();
//        HttpTool.init("");
        //直播流接收端口
        UdpServiceUntil udpService = new UdpServiceUntil();
        udpService.start(videoCmdUntil.rtpPort, new UDPRTPHandle());
        log.info("GB28181直播流UDP端口启动： {}", videoCmdUntil.rtpPort);
        //视频回放流端口
//        UdpServiceUntil udpBackService = new UdpServiceUntil();
//        udpBackService.start(videoCmdUntil.rtpBackPort, new UDPBackRTPHandle());
//        log.info("GB28181视频回放流UDP端口启动： {}", videoCmdUntil.rtpBackPort);
        //http端口 1.0
        httpNettyUntil.start(videoCmdUntil.httpPort);
        //HTTP 2.0 支持多路复用
//        http2NettyUntil.start(videoCmdUntil.httpPort);
        log.info("GB28181视频流HTTP长连接端口启动： {}", videoCmdUntil.httpPort);
        NettyConnectUntil.start();
        //检查进程
//        CloseThread.start();
        //UDP定时检测RTP流
        Gb28181RtpCachedThread.start();
        pushMsgQueue.start("pushMsgQueue");
    }

    @PreDestroy
    public void destroy() {
        httpNettyUntil.stop();
        NettyConnectUntil.stop();
        CloseThread.stop();
        Gb28181RtpCachedThread.stop();
        //检测进程
        pushMsgQueue.stop();
    }
}
